//
//  AgentCcsClientMessageDelegate.m
//  IVCS
//
//  Created by nfl on 2022/2/28.
//  Copyright © 2022 zco user. All rights reserved.
//

#import "AgentCcsClientMessageDelegate.h"

@implementation AgentCcsClientMessageDelegate

- (void)onCcsServerMessage:(NSArray * _Nonnull)data {
    NSDictionary *message = data.firstObject;
    ZcoModelCcsMessage *ccsMessage = [[ZcoModelCcsMessage alloc] initWithDictionary:message] ;
    
    SessionMessageType type = ccsMessage.type;
    NSDictionary *dataInfo = ccsMessage.data;
    if (!self.ivcsAgentDelegate) {
        return;
    }
    if ([type isEqualToString:SessionMessageTypePeerCancel]){
        ZcoModelCMD *cmd = [[ZcoModelCMD alloc] initWithDictionary:dataInfo];
        ccsMessage.data = cmd;
        if ([cmd.cmd isEqualToString:PeerCmdTypeScanCode]) {
            // 取消扫码
            if ([self.ivcsAgentDelegate respondsToSelector:@selector(onScanCode:withMessage:)]) {
//                [self.ivcsAgentDelegate onScanCode:@NO withMessage:ccsMessage];
            }
        }
    }
    else if([type isEqualToString:SessionMessageTypePeerText]){
        //文本消息
        if ([self.ivcsAgentDelegate respondsToSelector:@selector(onTextMessage:)]) {
            [self.ivcsAgentDelegate onTextMessage:ccsMessage];
        }
    }
    else if([type isEqualToString:SessionMessageTypePeerLink]){
        //资源文件消息
        if ([self.ivcsAgentDelegate respondsToSelector:@selector(onResourceData:)]) {
            ccsMessage.data = [[ZcoModelLinkInfo alloc] initWithDictionary:ccsMessage.data];
            [self.ivcsAgentDelegate onResourceData:ccsMessage];
        }
    }
    else if([type isEqualToString:SessionMessageTypeCustom]){
        //自定义消息
        if ([self.ivcsAgentDelegate respondsToSelector:@selector(onCustomMessage:)]) {
            [self.ivcsAgentDelegate onCustomMessage:ccsMessage];
        }
    }
    else if ([type isEqualToString:SessionMessageTypePeerStatus]){
        ZcoModelPeerStatusData *peerStatusData = [[ZcoModelPeerStatusData alloc] initWithDictionary:ccsMessage.data];
        ccsMessage.data = peerStatusData;
        if ([peerStatusData.status isEqualToString:@"quit"]) {
            if ([self.ivcsAgentDelegate respondsToSelector:@selector(onQuit:)]) {
                [self.ivcsAgentDelegate onQuit:ccsMessage];
            }
        }else if([peerStatusData.status isEqualToString:@"joined"]){
            if ([peerStatusData.role isEqualToString:@"agent"]) {
                
                if ([self.ivcsAgentDelegate respondsToSelector:@selector(onparticipantMessage:)]) {
                    [self.ivcsAgentDelegate onparticipantMessage:ccsMessage];
                }
            }
        }
       
    }
    else if ([type isEqualToString:SessionMessageTypePeerResponse]){
        if ([self.ivcsAgentDelegate respondsToSelector:@selector(onHandSignatureResponse:)]) {
            [self.ivcsAgentDelegate onHandSignatureResponse:ccsMessage];
        }
    }
    else if ([type isEqualToString:Workflow]){
        if ([self.ivcsAgentDelegate respondsToSelector:@selector(onWorkflow:)]) {
            [self.ivcsAgentDelegate onWorkflow:ccsMessage];
        }
    }
}

@end
